/*
 *   UltraBench -- Connectivity Benchmarking Utility
 *
 *      ultrabench.c - Main source file
 *
 *
 *   Copyright (C) 2009 Ultratronik Entwicklungs GmbH 
 *
 *     - Author: Sören Wellhöfer <soeren.wellhoefer@gmx.net>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *   MA 02110-1301, USA.
 */

#include "common.hpp"
#include "items.hpp"

#include <QtWi>
#include <QtWiAux>
#include <iostream>

extern "C" {

#include <unistd.h>

}

/////////////
// Globals //
/////////////

const char *progname = "UltraBench";
const char *version = strfy(ULTRABENCH_VERSION) "b";
const char *gsystem =
#if defined(QTWI_QT)
	"Qt";
#else
	"tnw";
#endif

Console *app_console;
IMutexLock<Console*> app_console_lock(app_console);
bool write_logfiles;
bool connected;


int main(int argc, char **argv)
{
	using namespace std;
	
	Ifactory *f;

#if defined(QTWI_QT)
	f = new QIfactory;
#elif defined(QTWI_TNW)
	f = new TIfactory;
#else
# error No graphics library specified. Define QTWI_QT or QTWI_TNW.
#endif
	
	IApplication *app = f->makeApplication(argc, argv);
	f->setApp(app);

	try {
		
		//// Main widgets placement ////
		
		// Window
		IWindow *window = f->mainWindow(progname, 10, 10);
		window->resize(main_window_width, main_window_height);
		
		if(geteuid() != 0) {
			IStandardMonologue root(*f, "Error", 30, 30, "Please run as root.");
			window->hide();
			return app->exec();
		}
		
		// Entire window must be enveloped by raw widget
		IRawWidget *raw = f->makeRawWidget(window, "", 0, 0);
		raw->resize(main_window_width, main_window_height);
		usleep(messy_concurrency_fixup << 2);
		
		// Usage bars
		UsageBars usg_bars(*raw, *f, main_widgets_xoffset, 10, main_widgets_width, 110);
		
		// Gui connector
		GuiConnector gui_cnt(*raw, *f, main_widgets_xoffset, 110, main_widgets_width, GuiConnector::gui_connector_overall_height);

		// Console
		IRadioGroup *console_frame = f->makeRadioGroup(raw, "Console", main_widgets_xoffset, 330);
		console_frame->resize(main_widgets_width, 83);
		Console c(*console_frame, *f, 7, 15, main_widgets_width - 15, 62);
		app_console = &c;
		//console_frame->show();
		
		//// Code ////
		c << Console::note << progname  << " v. " <<
			version << " (" << gsystem << ") initialized" << Console::endl;
		
		window->show();
		raw->show();

		return app->exec();

	} catch(IException e) {
		std::cerr << e.info() << std::endl;
		return -1;
	};
}
